package io.geekidea.boot.shop.service.base.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.geekidea.boot.framework.exception.BusinessException;
import io.geekidea.boot.framework.page.OrderByItem;
import io.geekidea.boot.framework.page.OrderMapping;
import io.geekidea.boot.framework.page.Paging;
import io.geekidea.boot.shop.dto.BannerDto;
import io.geekidea.boot.shop.entity.Banner;
import io.geekidea.boot.shop.mapper.BannerMapper;
import io.geekidea.boot.shop.query.app.AppBannerQuery;
import io.geekidea.boot.shop.query.web.BannerQuery;
import io.geekidea.boot.shop.service.base.api.BannerService;
import io.geekidea.boot.shop.vo.app.AppBannerVo;
import io.geekidea.boot.shop.vo.web.BannerVo;
import io.geekidea.boot.util.PagingUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 *  服务实现类
 *
 * @author geekidea
 * @since 2024-10-27
 */
@Slf4j
@Service
public class BannerServiceImpl extends ServiceImpl<BannerMapper, Banner> implements BannerService {

    @Autowired
    private BannerMapper bannerMapper;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean addBanner(BannerDto dto) {
        Banner banner = new Banner();
        BeanUtils.copyProperties(dto, banner);
        return save(banner);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean updateBanner(BannerDto dto) {
        Integer id = dto.getId();
        Banner banner = getById(id);
        if (banner == null) {
            throw new BusinessException("不存在");
        }
        BeanUtils.copyProperties(dto, banner);
        return updateById(banner);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean deleteBanner(Integer id) {
        return removeById(id);
    }

    @Override
    public BannerVo getBannerById(Integer id) {
        return bannerMapper.getBannerById(id);
    }

    @Override
    public Paging<BannerVo> getBannerPage(BannerQuery query) {
        OrderMapping orderMapping = new OrderMapping();
        orderMapping.put("createTime", "create_time");
        PagingUtil.handlePage(query, orderMapping, OrderByItem.desc("id"));
        List<BannerVo> list = bannerMapper.getBannerPage(query);
        Paging<BannerVo> paging = new Paging<>(list);
        return paging;
    }

    @Override
    public AppBannerVo getAppBannerById(Integer id) {
        return bannerMapper.getAppBannerById(id);
    }

    @Override
    public Paging<AppBannerVo> getAppBannerPage(AppBannerQuery query) {
        OrderMapping orderMapping = new OrderMapping();
        orderMapping.put("createTime", "create_time");
        PagingUtil.handlePage(query, orderMapping, OrderByItem.desc("id"));
        List<AppBannerVo> list = bannerMapper.getAppBannerPage(query);
        Paging<AppBannerVo> paging = new Paging<>(list);
        return paging;
    }

}
